package com.example.myapplication.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.example.myapplication.model.Fruit;
import com.example.myapplication.MyAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class AnimationFragment extends Fragment implements View.OnClickListener {

    private View mRootView;
    private List<Fruit> list;
    private MyAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_animation_layout,
                container,
                false);

        list = new ArrayList<>();

        list.add( new Fruit(R.drawable.a, "苹果", 1.0f) );
        list.add( new Fruit(R.drawable.b, "草莓", 2.0f) );
        list.add( new Fruit(R.drawable.c, "樱桃", 3.0f) );
        list.add( new Fruit(R.drawable.d, "桔子", 4.0f) );

        ListView lv = mRootView.findViewById(R.id.list_view);
        adapter = new MyAdapter(list, getActivity());
        lv.setAdapter(adapter);


        Button btnAdd = mRootView.findViewById(R.id.btn_add);
        Button btnDel = mRootView.findViewById(R.id.btn_del);
        btnAdd.setOnClickListener(this);
        btnDel.setOnClickListener(this);

        LottieAnimationView anim1 = mRootView.findViewById(R.id.animation1);
        LottieAnimationView anim2 = mRootView.findViewById(R.id.animation2);

        anim1.setAnimation(R.raw.adrock);
        anim2.setAnimation(R.raw.navidad);

        anim1.playAnimation();
        anim2.playAnimation();

        anim1.setRepeatCount(LottieDrawable.INFINITE);
        anim2.setRepeatCount(LottieDrawable.INFINITE);

        return mRootView;
    }

    private int count = 0;

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_add) {
            switch (count++%4){
                case 0:
                    list.add( new Fruit(R.drawable.a, "苹果", 1.0f) );
                    break;
                case 1:
                    list.add( new Fruit(R.drawable.b, "草莓", 2.0f) );
                    break;
                case 2:
                    list.add(new Fruit(R.drawable.c, "樱桃", 3.0f));
                    break;
                case 3:
                    list.add( new Fruit(R.drawable.d, "桔子", 4.0f) );
                    break;
            }
            adapter.notifyDataSetChanged();
        } else if(v.getId() == R.id.btn_del) {
            if(list.size() > 0) {
                list.remove(list.size()-1);
                adapter.notifyDataSetChanged();
            }
        }
    }
}
